<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>xv6问题集合 | kiloGrand</title><meta name="keywords" content="xv6"><meta name="author" content="kiloGrand"><meta name="copyright" content="kiloGrand"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="进程表在哪 进程切换的过程  spinlock 为什么spinlock中可以直接使用了push_off和pop_off来关闭&#x2F;开启中断，而不需要进入内核？spinlock中使用了push_off和pop_off来关闭&#x2F;开启中断，这个中断包括外部中断和定时中断吗？如果一个用户进程acquire陷入死循环，那么中断是否永远关闭？如果一个用户进程acquire后还没有release，而另一个进程rel">
<meta property="og:type" content="article">
<meta property="og:title" content="xv6问题集合">
<meta property="og:url" content="https://kilogrand.gitee.io/2022/09/11/xv6-questions/index.html">
<meta property="og:site_name" content="kiloGrand">
<meta property="og:description" content="进程表在哪 进程切换的过程  spinlock 为什么spinlock中可以直接使用了push_off和pop_off来关闭&#x2F;开启中断，而不需要进入内核？spinlock中使用了push_off和pop_off来关闭&#x2F;开启中断，这个中断包括外部中断和定时中断吗？如果一个用户进程acquire陷入死循环，那么中断是否永远关闭？如果一个用户进程acquire后还没有release，而另一个进程rel">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://kilogrand.gitee.io/img/coding.jpg">
<meta property="article:published_time" content="2022-09-11T12:41:10.000Z">
<meta property="article:modified_time" content="2023-04-03T12:47:50.219Z">
<meta property="article:author" content="kiloGrand">
<meta property="article:tag" content="xv6">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kilogrand.gitee.io/img/coding.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kilogrand.gitee.io/2022/09/11/xv6-questions/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'xv6问题集合',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-03 20:47:50'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/profile.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/coding.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">kiloGrand</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">xv6问题集合</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-09-11T12:41:10.000Z" title="发表于 2022-09-11 20:41:10">2022-09-11</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-03T12:47:50.219Z" title="更新于 2023-04-03 20:47:50">2023-04-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>5分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="xv6问题集合"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>进程表在哪</p>
<p>进程切换的过程</p>
</blockquote>
<h2 id="spinlock"><a href="#spinlock" class="headerlink" title="spinlock"></a>spinlock</h2><blockquote>
<p>为什么spinlock中可以直接使用了push_off和pop_off来关闭/开启中断，而不需要进入内核？<br>spinlock中使用了push_off和pop_off来关闭/开启中断，这个中断包括外部中断和定时中断吗？<br>如果一个用户进程acquire陷入死循环，那么中断是否永远关闭？<br>如果一个用户进程acquire后还没有release，而另一个进程release锁，并且这两个进程的锁不是同一个，那么中断是否开启了？</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">push_off</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="type">int</span> old = intr_get();  <span class="comment">// 获取是否开启中断</span></span><br><span class="line">  <span class="comment">// noff: num of interrupts,   intena: interrupts enable</span></span><br><span class="line">  intr_off();  <span class="comment">// 关闭中断</span></span><br><span class="line">  <span class="keyword">if</span>(mycpu()-&gt;noff == <span class="number">0</span>)  <span class="comment">// 如果是第一次关闭中断</span></span><br><span class="line">    mycpu()-&gt;intena = old;  <span class="comment">// 记录上一次是否开启中断</span></span><br><span class="line">  mycpu()-&gt;noff += <span class="number">1</span>;  <span class="comment">// 中断次数加一</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">pop_off</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="class"><span class="keyword">struct</span> <span class="title">cpu</span> *<span class="title">c</span> =</span> mycpu();  <span class="comment">// 获取CPU</span></span><br><span class="line">  <span class="keyword">if</span>(intr_get())   <span class="comment">// 是否开启中断</span></span><br><span class="line">    panic(<span class="string">&quot;pop_off - interruptible&quot;</span>);</span><br><span class="line">  <span class="keyword">if</span>(c-&gt;noff &lt; <span class="number">1</span>) <span class="comment">// 中断次数是否≤0，意味着没有中断</span></span><br><span class="line">    panic(<span class="string">&quot;pop_off&quot;</span>);</span><br><span class="line">  c-&gt;noff -= <span class="number">1</span>;  <span class="comment">// 中断次数减一</span></span><br><span class="line">  <span class="keyword">if</span>(c-&gt;noff == <span class="number">0</span> &amp;&amp; c-&gt;intena)  <span class="comment">// 如果中断次数变为0并且之前已经开启过中断</span></span><br><span class="line">    intr_on();  <span class="comment">// 开启中断</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SSTATUS_SIE (1L &lt;&lt; 1)  <span class="comment">// Supervisor Interrupt Enable</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> uint64</span><br><span class="line"><span class="title function_">r_sstatus</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">  uint64 x;</span><br><span class="line">  <span class="keyword">asm</span> <span class="title function_">volatile</span><span class="params">(<span class="string">&quot;csrr %0, sstatus&quot;</span> : <span class="string">&quot;=r&quot;</span> (x) )</span>;</span><br><span class="line">  <span class="keyword">return</span> x;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> </span><br><span class="line"><span class="title function_">w_mstatus</span><span class="params">(uint64 x)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">asm</span> <span class="title function_">volatile</span><span class="params">(<span class="string">&quot;csrw mstatus, %0&quot;</span> : : <span class="string">&quot;r&quot;</span> (x))</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// enable device interrupts</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span></span><br><span class="line"><span class="title function_">intr_on</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">  w_sstatus(r_sstatus() | SSTATUS_SIE);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// disable device interrupts</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span></span><br><span class="line"><span class="title function_">intr_off</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">  w_sstatus(r_sstatus() &amp; ~SSTATUS_SIE);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在中断使能方面，MIE 、SIE 、UIE 分别提供了 machine mode 、supervisor mode 、user mode 的全局中断使能位；<br>User级中运行用户程序，Supervisor级中运行操作系统内核（和设备驱动），Machine级中运行BootLoader和其它固件。</p>
<ul>
<li>machine mode 拥有所有的特权，一般在启动时候用于配置电脑的环境</li>
<li>supervisor mode 的权限相对低些，可以执行特权指令，例如是否使能中断等</li>
<li>user mode 的权限级别最低，完成一些特殊功能的时候需要通过系统调用进入 supervisor mode</li>
<li>在 RISC-V 中，通过寄存器 sstatus 中保存的 SPP 位来判断是处于内核态 (1) 还是用户态 (0)</li>
<li><p>中断分为外中断和内中断，外中断包括 I/O 中断、时钟中断等，内中断包括异常、系统调用和中止。</p>
</li>
<li><p><a target="_blank" rel="noopener" href="http://rcore-os.cn/rCore-Tutorial-deploy/docs/lab-1/guide/part-2.html">http://rcore-os.cn/rCore-Tutorial-deploy/docs/lab-1/guide/part-2.html</a></p>
</li>
<li><a target="_blank" rel="noopener" href="https://dingfen.github.io/risc-v/2020/08/05/riscv-privileged.html">https://dingfen.github.io/risc-v/2020/08/05/riscv-privileged.html</a></li>
</ul>
<h2 id="kalloc"><a href="#kalloc" class="headerlink" title="kalloc"></a>kalloc</h2><p>在kinit中freerange中end的地址是多少？<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> KERNBASE 0x80000000L</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PHYSTOP (KERNBASE + 128*1024*1024)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PGSIZE 4096 <span class="comment">// bytes per page</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PGROUNDUP(sz)  (((sz)+PGSIZE-1) &amp; ~(PGSIZE-1))</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="type">char</span> end[]; <span class="comment">// first address after kernel.</span></span><br><span class="line">                   <span class="comment">// defined by kernel.ld.</span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">kinit</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">  initlock(&amp;kmem.lock, <span class="string">&quot;kmem&quot;</span>);</span><br><span class="line">  freerange(end, (<span class="type">void</span>*)PHYSTOP);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">freerange</span><span class="params">(<span class="type">void</span> *pa_start, <span class="type">void</span> *pa_end)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="type">char</span> *p;</span><br><span class="line">  p = (<span class="type">char</span>*)PGROUNDUP((uint64)pa_start);</span><br><span class="line">  <span class="keyword">for</span>(; p + PGSIZE &lt;= (<span class="type">char</span>*)pa_end; p += PGSIZE)</span><br><span class="line">    kfree(p);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="每个CPU都有自己的定时中断吗？"><a href="#每个CPU都有自己的定时中断吗？" class="headerlink" title="每个CPU都有自己的定时中断吗？"></a>每个CPU都有自己的定时中断吗？</h2><h2 id="trap是怎么跳转到trampoline中的usertrap-的？"><a href="#trap是怎么跳转到trampoline中的usertrap-的？" class="headerlink" title="trap是怎么跳转到trampoline中的usertrap()的？"></a>trap是怎么跳转到trampoline中的usertrap()的？</h2><p>riscv为处理trap提供了sstatuts、scause、stvec、sepc、sscratch等寄存器，这些寄存器都会被中断隐指令所使用到：<br>（1）stvec寄存器存放着trap wrapper的地址。当trap发生时，stvec的值将被读入到pc中<br>（2）scause记录这个trap的发生原因。当trap发生时，这个寄存器被设置<br>（3）sepc保存pc的值。在PC的值被更新为stvec之前，PC值被保存在这个寄存器中<br>（4）sstatuts的SPP位记录着当前CPU所处的mode。<br>（5）sscratch记录着这个进程的trapframe地址。保存现场时，需要将寄存器保存在trapframe上。<br>总而言之，当trap发生时，首先会“执行中断隐指令”，完成关中断和设定trap相关寄存器等操作，而保护现场等操作，是由相应的指令执行的。</p>
<p>在内核初始化的时候，会设置stvec寄存器，因此将中断处理入口设置成了kernelvec。<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">trapinithart</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">  w_stvec((uint64)kernelvec);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="在内核态的时候是怎么trap的？"><a href="#在内核态的时候是怎么trap的？" class="headerlink" title="在内核态的时候是怎么trap的？"></a>在内核态的时候是怎么trap的？</h2><p>kernelvec、kerneltrap</p>
<h2 id="trampoline是什么"><a href="#trampoline是什么" class="headerlink" title="trampoline是什么"></a>trampoline是什么</h2><p>trampoline是跳板代码，包含以下这两段汇编：<br>1.uservec的作用是保存现在进程的栈帧，恢复kernel的tp、satp，切换到usertrap()；usertrap()中通过yield()切换到调度器线程。<br>2.userret的作用是恢复用户的satp，恢复进程的栈帧，切换到usertrapret()。</p>
<h2 id="为什么调度器是一个线程，而不是一个进程"><a href="#为什么调度器是一个线程，而不是一个进程" class="headerlink" title="为什么调度器是一个线程，而不是一个进程"></a>为什么调度器是一个线程，而不是一个进程</h2><p>首先要了解线程切换和进程切换要保存和恢复什么，<br>对于线程来说只需要保存栈帧（寄存器现场）就行，<br>对于进程来说要保存PCB（PCB中包含栈帧、打开的文件描述符等）、需要重新映射虚拟地址空间、进出OS内核、寄存器切换，<br>因此进程切换的代价要高于线性切换的代价。<br>在xv6中，通过保存和恢复栈帧来切换到调度器，并且调度器没有对应的PCB，所有调度器是一个线程。</p>
<h2 id="在-usertrap函数中-p-gt-trapframe-gt-epc-4"><a href="#在-usertrap函数中-p-gt-trapframe-gt-epc-4" class="headerlink" title="在  usertrap函数中 p-&gt;trapframe-&gt;epc += 4;"></a>在  usertrap函数中 p-&gt;trapframe-&gt;epc += 4;</h2><p>epc = epc + 4，epc下一条指令是什么？是不是ecall的下一条指令？<br>syscall，ecall的下一条指令是ret</p>
<h2 id="trap-page是什么？有什么？"><a href="#trap-page是什么？有什么？" class="headerlink" title="trap page是什么？有什么？"></a>trap page是什么？有什么？</h2><h2 id="为什么fork父进程返回子进程的PID而子进程返回0？"><a href="#为什么fork父进程返回子进程的PID而子进程返回0？" class="headerlink" title="为什么fork父进程返回子进程的PID而子进程返回0？"></a>为什么fork父进程返回子进程的PID而子进程返回0？</h2><p>对于父进程来说，fork返回子进程的pid，而子进程没有拷贝a0，而是把a0设成0，所以对于子进程来说返回0.</p>
<h2 id="proc-c里面有exit-（sys-exit调用exit），提供给用户的系统调用接口也是exit，不会冲突吗？"><a href="#proc-c里面有exit-（sys-exit调用exit），提供给用户的系统调用接口也是exit，不会冲突吗？" class="headerlink" title="proc.c里面有exit()（sys_exit调用exit），提供给用户的系统调用接口也是exit，不会冲突吗？"></a>proc.c里面有exit()（sys_exit调用exit），提供给用户的系统调用接口也是exit，不会冲突吗？</h2><p>同样的sys_wait调用wait，而提供给用户的系统调用接口也是wait，不会冲突吗？<br>同样的sys_kill调用kill，而提供给用户的系统调用接口也是kill，不会冲突吗？</p>
<h2 id="文件描述符是不是proc结构体里面ofile（open-file-array）的下标？"><a href="#文件描述符是不是proc结构体里面ofile（open-file-array）的下标？" class="headerlink" title="文件描述符是不是proc结构体里面ofile（open file array）的下标？"></a>文件描述符是不是proc结构体里面ofile（open file array）的下标？</h2><p>是的，ofile[i]指向file struct，如果file struct的类型是fd_inode，那么file struct中的ip指向inde。<br>内核中有一个大小为100的ftable数组。</p>
<h2 id="exec执行成功会不会有return？如果没有，那么是什么原因？"><a href="#exec执行成功会不会有return？如果没有，那么是什么原因？" class="headerlink" title="exec执行成功会不会有return？如果没有，那么是什么原因？"></a>exec执行成功会不会有return？如果没有，那么是什么原因？</h2><p>sys_exec和exec函数。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io">kiloGrand</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io/2022/09/11/xv6-questions/">https://kilogrand.gitee.io/2022/09/11/xv6-questions/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kilogrand.gitee.io" target="_blank">kiloGrand</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/xv6/">xv6</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/09/10/xv6-lab11/"><img class="prev-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">xv6 实验十一 networking</div></div></a></div><div class="next-post pull-right"><a href="/2022/10/01/nlp-sentiment-analysis-dataset/"><img class="next-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">情感分析常见数据集介绍</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/08/31/xv6-Chapter-0/" title="xv6 riscv book 中文版声明"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 riscv book 中文版声明</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-1/" title="xv6 第一章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第一章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-2/" title="xv6 第二章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第二章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-3/" title="xv6 第三章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第三章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-4/" title="xv6 第四章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第四章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-5/" title="xv6 第五章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第五章</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/profile.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">kiloGrand</div><div class="author-info__description">coder && data-science researcher</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/kiloGrand/"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#spinlock"><span class="toc-number">1.</span> <span class="toc-text">spinlock</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#kalloc"><span class="toc-number">2.</span> <span class="toc-text">kalloc</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AF%8F%E4%B8%AACPU%E9%83%BD%E6%9C%89%E8%87%AA%E5%B7%B1%E7%9A%84%E5%AE%9A%E6%97%B6%E4%B8%AD%E6%96%AD%E5%90%97%EF%BC%9F"><span class="toc-number">3.</span> <span class="toc-text">每个CPU都有自己的定时中断吗？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#trap%E6%98%AF%E6%80%8E%E4%B9%88%E8%B7%B3%E8%BD%AC%E5%88%B0trampoline%E4%B8%AD%E7%9A%84usertrap-%E7%9A%84%EF%BC%9F"><span class="toc-number">4.</span> <span class="toc-text">trap是怎么跳转到trampoline中的usertrap()的？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8%E5%86%85%E6%A0%B8%E6%80%81%E7%9A%84%E6%97%B6%E5%80%99%E6%98%AF%E6%80%8E%E4%B9%88trap%E7%9A%84%EF%BC%9F"><span class="toc-number">5.</span> <span class="toc-text">在内核态的时候是怎么trap的？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#trampoline%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-number">6.</span> <span class="toc-text">trampoline是什么</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%B0%83%E5%BA%A6%E5%99%A8%E6%98%AF%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AF%E4%B8%80%E4%B8%AA%E8%BF%9B%E7%A8%8B"><span class="toc-number">7.</span> <span class="toc-text">为什么调度器是一个线程，而不是一个进程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8-usertrap%E5%87%BD%E6%95%B0%E4%B8%AD-p-gt-trapframe-gt-epc-4"><span class="toc-number">8.</span> <span class="toc-text">在  usertrap函数中 p-&gt;trapframe-&gt;epc +&#x3D; 4;</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#trap-page%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F%E6%9C%89%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="toc-number">9.</span> <span class="toc-text">trap page是什么？有什么？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88fork%E7%88%B6%E8%BF%9B%E7%A8%8B%E8%BF%94%E5%9B%9E%E5%AD%90%E8%BF%9B%E7%A8%8B%E7%9A%84PID%E8%80%8C%E5%AD%90%E8%BF%9B%E7%A8%8B%E8%BF%94%E5%9B%9E0%EF%BC%9F"><span class="toc-number">10.</span> <span class="toc-text">为什么fork父进程返回子进程的PID而子进程返回0？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#proc-c%E9%87%8C%E9%9D%A2%E6%9C%89exit-%EF%BC%88sys-exit%E8%B0%83%E7%94%A8exit%EF%BC%89%EF%BC%8C%E6%8F%90%E4%BE%9B%E7%BB%99%E7%94%A8%E6%88%B7%E7%9A%84%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%E6%8E%A5%E5%8F%A3%E4%B9%9F%E6%98%AFexit%EF%BC%8C%E4%B8%8D%E4%BC%9A%E5%86%B2%E7%AA%81%E5%90%97%EF%BC%9F"><span class="toc-number">11.</span> <span class="toc-text">proc.c里面有exit()（sys_exit调用exit），提供给用户的系统调用接口也是exit，不会冲突吗？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E6%8F%8F%E8%BF%B0%E7%AC%A6%E6%98%AF%E4%B8%8D%E6%98%AFproc%E7%BB%93%E6%9E%84%E4%BD%93%E9%87%8C%E9%9D%A2ofile%EF%BC%88open-file-array%EF%BC%89%E7%9A%84%E4%B8%8B%E6%A0%87%EF%BC%9F"><span class="toc-number">12.</span> <span class="toc-text">文件描述符是不是proc结构体里面ofile（open file array）的下标？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#exec%E6%89%A7%E8%A1%8C%E6%88%90%E5%8A%9F%E4%BC%9A%E4%B8%8D%E4%BC%9A%E6%9C%89return%EF%BC%9F%E5%A6%82%E6%9E%9C%E6%B2%A1%E6%9C%89%EF%BC%8C%E9%82%A3%E4%B9%88%E6%98%AF%E4%BB%80%E4%B9%88%E5%8E%9F%E5%9B%A0%EF%BC%9F"><span class="toc-number">13.</span> <span class="toc-text">exec执行成功会不会有return？如果没有，那么是什么原因？</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/24/kuiper_infer-L14/" title="自制深度学习框架--实现Yolov5的推理">自制深度学习框架--实现Yolov5的推理</a><time datetime="2023-03-24T12:00:00.000Z" title="发表于 2023-03-24 20:00:00">2023-03-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理">自制深度学习框架--实现ResNet网络的推理</a><time datetime="2023-03-23T12:00:00.000Z" title="发表于 2023-03-23 20:00:00">2023-03-23</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程">自制深度学习框架--算子的执行流程</a><time datetime="2023-03-22T12:00:00.000Z" title="发表于 2023-03-22 20:00:00">2023-03-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系">自制深度学习框架--再探Tensor类并构建计算图的图关系</a><time datetime="2023-03-21T12:00:00.000Z" title="发表于 2023-03-21 20:00:00">2023-03-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现">自制深度学习框架--Im2Col原理与卷积层的实现</a><time datetime="2023-03-20T12:00:00.000Z" title="发表于 2023-03-20 20:00:00">2023-03-20</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By kiloGrand</div><div class="footer_custom_text">Hi, welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>